/*
 * WorldViewControl.cpp
 *
 *  Created on: 27-12-2010
 *      Author: Przemek
 */

#include "customUI/WorldViewControl.hpp"
#include "world/World.hpp"
#include "world/visitors/DrawerVisitor.hpp"
#include "utils/GLUtils.hpp"
#include "pch.hpp"
using namespace boost;

const GLColor WorldViewControl::BACKGROUND(0.0f, 0.5f, 0.0f);

WorldViewControl::WorldViewControl(SelectElementMouseListener* selectElementMouseListener,
		CreationState* creationState) :
	selectElementMouseListener(selectElementMouseListener), creationState(creationState)
{
	addManagedMouseListener(selectElementMouseListener);
	setSize(World::BOUNDS.getSize());
}

void WorldViewControl::draw(const ClipArea& clipArea)
{
	BACKGROUND.setGLState();
	clipArea.setGLClipArea();
	GLUtils::drawRectangle(Vector(0, 0), getSize());

	DrawerVisitor drawer;
	World::ElementsVector elements = World::getInstance().getElements();
	BOOST_FOREACH(const shared_ptr<Element>& element, elements)
				{
					element->accept(drawer);
				}
	if(creationState->anyToAdd())
	{
		creationState->getToAdd()->accept(drawer);
	}
	if(selectElementMouseListener->anySelected())
	{
		drawer.setSelectedElementCenter(
				selectElementMouseListener->getSelected()->getBounds().getCenter());

	}
	drawer.flush();
}

